package cn.kgc.airmanage.demo.controller;


import cn.kgc.airmanage.demo.ResultEntity;
import cn.kgc.airmanage.demo.pojo.Airqualityindex;
import cn.kgc.airmanage.demo.pojo.District;
import cn.kgc.airmanage.demo.pojo.Viewdistrictair;
import cn.kgc.airmanage.demo.service.AirqualityService;
import cn.kgc.airmanage.demo.service.DistrictService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

@RestController
public class AirController {
    @Resource
    AirqualityService service1;
    @Resource
    DistrictService service2;

    /*显示区域*/
    @RequestMapping("/getSelectDistrict")
    public ResultEntity<List<District>> getSelectDistrict(){
        List<District> list=service2.SelectName();
        return ResultEntity.successWithoutData(list);
    }
    /*显示空气质量*/
    @RequestMapping("/getSelectAirBydisid")
    public ResultEntity<PageInfo<Viewdistrictair>> getSelectAirBydisid(@RequestParam(value = "districtid",required = false,defaultValue = "0") Integer districtid,
                                                                       @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                                                                       @RequestParam(value = "pageSize",required = false,defaultValue = "5") Integer pageSize){

        PageInfo<Viewdistrictair> pageInfo=service1.SelectAllBydistrictId(districtid, pageNum, pageSize);
        return ResultEntity.successWithoutData(pageInfo);
    }
    /*添加空气质量*/
    @RequestMapping("/saveAir")
    public ResultEntity<Integer> saveAir(@RequestBody Airqualityindex airqualityindex){
        airqualityindex.setLastmodifytime(new Date());
        if(airqualityindex.getId()==null){//添加
            Integer result=service1.AddAir(airqualityindex);
            return ResultEntity.successWithoutData(result);
        }else{//修改
            Integer result=service1.UpdateAir(airqualityindex);
            return ResultEntity.successWithoutData(result);
        }
    }

    /*根据id删除*/
    @RequestMapping("/delAirByid")
    public ResultEntity<Integer> getdelAirByid(Integer id){
        Integer result=service1.DeleteAir(id);
        return ResultEntity.successWithoutData(result);
    }
    /*根据Id查询*/
    @RequestMapping("/SelectByid")
    public ResultEntity<Airqualityindex> getSelecttAirByid(Integer id) throws ParseException {
        Airqualityindex airqualityindex=service1.SelectByid(id);
        return ResultEntity.successWithoutData(airqualityindex);
    }
}
